<template>
  <div id="app">
    <header class="header">
      <button @click="show = !show">按钮</button>
    </header>
    <main class="main">
      <LeftCom
        :typeArr="typeArr"
        :tabIndex="tabIndex"
        @tabClick="tabClick"
      ></LeftCom>
      <RightCom v-if="show" :list="list"></RightCom>
    </main>
    <footer class="footer"></footer>
  </div>
</template>

<script>
import axios from 'axios'
import LeftCom from './components/Left.vue'
import RightCom from './components/Right.vue'
export default {
  name: 'App',
  components: { LeftCom, RightCom },
  data() {
    return {
      type: '热门推荐',
      typeArr: [],
      list: [],
      tabIndex: 0,
      show: false,
      timer: null,
    }
  },
  // beforeCreate() {
  //   console.log('beforeCreate===>', this.$el)
  // },
  created() {
    this.getData()
  },
  // beforeMount() {
  //   console.log('beforeMount:', this.$el)
  // },
  mounted() {
    // 操作dom
    // console.log('mounted:->', this.$el)
  },
  // beforeUpdate() {
  //   console.log('更新之前 beforeUpdate:', this.$el)
  // },
  updated() {
    // 跟watch有点类似
    // console.log('updated:', this.tabIndex)
    // console.log('更新阶段 updated:', this.$el)
  },
  // beforeDestroy() {
  //   console.log('beforeDestroy', this.$el)
  // },
  destroyed() {
    // 往往做一些销毁 内存中存在的变量的行为 清除定时器
    // console.log('组件销毁生命周期', this.$el)
  },

  methods: {
    async getData() {
      // console.log(this.type, 'type-------')
      const res = await axios.get('/api/list', { params: { type: this.type } })
      // console.log('res:', res.data.data)
      const data = res.data.data
      this.typeArr = data.typeArr
      this.list = data.list[0].children
    },
    tabClick({ index, item }) {
      this.tabIndex = index
      // 更改请求的参数
      this.type = item
      this.getData()
    },
  },
}
</script>

<style lang="scss">
* {
  margin: 0;
  padding: 0;
  list-style: none;
  box-sizing: border-box;
}
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
}
.header {
  height: 50px;
  border-bottom: 1px solid #000;
}
.main {
  flex: 1;
  overflow: auto;
  display: flex;
  .left {
    flex: 2;
    border-right: 1px solid #ccc;
    span {
      display: block;
      margin: 5px;
      // padding: 5px;
      width: 90%;
      height: 35px;
      line-height: 35px;
      text-align: center;
      border: 1px solid #ccc;
      border-radius: 5px;
      &.active {
        background: red;
        color: gold;
      }
    }
  }
  .right {
    flex: 8;
    display: flex;
    flex-wrap: wrap;
    ul {
      flex: 1;
      padding: 5px;
      margin: 5px;
    }
  }
}
.footer {
  height: 50px;
  border-top: 1px solid #000;
}
</style>
